<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        var theObject = {
            name: '测试对象',
        }
        var wfObj = new WeakRef(theObject);
        var map = new Map();
        map.set(wfObj, {
            someData: "...."
        })
        const registry = new FinalizationRegistry(({ wfObj }) => {
            console.log("被回收了");
            map.delete(wfObj);
            console.log("map.size:", map.size);
        });
        registry.register(theObject, { wfObj });
        setTimeout(() => {
            window.gc();

            theObject = null;
        }, 100);
    </script>
</body>

</html>l